package com.opmsys.mapper;

import com.opmsys.pojo.entity.Pumping;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PumpingMapper {
    /**
     * 新增抽油机
     * @param pumping
     */
    @Insert("INSERT INTO t_pumping (unit_model, production_date, deployment_date, unit_type,max_load, unit_size, unit_power, production, unit_status, well_id) " +
            "VALUES (#{pumping.unitModel}, #{pumping.productionDate}, #{pumping.deploymentDate}, #{pumping.unitType}, #{pumping.maxLoad}," +
            "#{pumping.unitSize}, #{pumping.unitPower}, #{pumping.production}, #{pumping.unitStatus}, #{pumping.wellId})")
    void insertPumping(@Param("pumping") Pumping pumping);

    /**
     * 删除抽油机
     * @param id
     */
    @Delete("DELETE FROM t_pumping WHERE id = #{id}")
    void deletePumping(@Param("id") Integer id);

    /**
     * 更新抽油机
     * @param pumping
     */
    @Update("<script>" +
            "UPDATE t_pumping " +
            "<set>" +
            "<if test='pumping.unitModel != null and pumping.unitModel != \"\" '> unit_model = #{pumping.unitModel}, </if>" +
            "<if test='pumping.productionDate != null and pumping.productionDate != \"\" '> production_date = #{pumping.productionDate}, </if>" +
            "<if test='pumping.deploymentDate != null and pumping.deploymentDate != \"\" '> deployment_date = #{pumping.deploymentDate}, </if>" +
            "<if test='pumping.unitType != null and pumping.unitType != \"\" '> unit_type = #{pumping.unitType}, </if>" +
            "<if test='pumping.maxLoad != null'> max_load = #{pumping.maxLoad}, </if>" +
            "<if test='pumping.unitSize != null and pumping.unitSize != \"\" '> unit_size = #{pumping.unitSize}, </if>" +
            "<if test='pumping.unitPower != null and pumping.unitPower != \"\" '> unit_power = #{pumping.unitPower}, </if>" +
            "<if test='pumping.production != null'> production = #{pumping.production}, </if>" +
            "<if test='pumping.unitStatus != null'> unit_status = #{pumping.unitStatus}, </if>" +
            "<if test='pumping.wellId != null'> well_id = #{pumping.wellId}, </if>" +
            "</set> " +
            "WHERE id = #{pumping.id}" +
            "</script>")
    void updatePumping(@Param("pumping") Pumping pumping);

    /**
     * 模糊模糊查询抽油机并分页
     * @param characteristic
     * @param pageSize
     * @param offset
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_pumping " +
            "<where>" +
            "<if test='characteristic != null'> AND unit_model LIKE CONCAT('%', #{characteristic}, '%') </if>" +
            "</where> " +
            "ORDER BY id " +
            "LIMIT #{pageSize} OFFSET #{offset}"+
            "</script>")
    List<Pumping> listPumpings(@Param("characteristic") String characteristic,@Param("pageSize") Integer pageSize, @Param("offset") Integer offset);

    /**
     * 模糊模糊查询抽油机
     * @param characteristic
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_pumping " +
            "<where>" +
            "<if test='characteristic != null'> AND unit_model LIKE CONCAT('%', #{characteristic}, '%') </if>" +
            "</where> " +
            "ORDER BY id " +
            "</script>")
    List<Pumping> searchPumpings(@Param("characteristic") String characteristic);


    @Select("select * from t_pumping where id = #{id}")
    Pumping selectById(Integer id);

    @Update("update t_pumping set well_id = null where well_id = #{id}")
    void setWellNull(Integer id);
}
